<?php

include_once('eleve.class.php');
include_once('adulte.class.php');
include_once('myPDO.class.php');

class EleveDAO {

    // Création d'un adulte
    public function createEleve(/* Eleve */ $eleve, /* myPDO */ $db) {
        try {


            $stmt = $db->prepare("INSERT INTO Etudiant (nom, prenom,mdp, imgProfil,Adulte_identifiant) VALUES (:nom, :prenom, :mdp,:img,:adulte);");

            $nom = utf8_decode($eleve->getNom());
            $prenom = utf8_decode($eleve->getPrenom());
            $mdp = utf8_decode($eleve->getPassword());
            $img = utf8_decode($eleve->getImage());
			$adulte = utf8_decode($eleve->getAdulte());
            $stmt->bindParam(':nom', $nom);
            $stmt->bindParam(':prenom', $prenom);
            $stmt->bindParam(':mdp', $mdp);
            $stmt->bindParam(':img', $img);
			$stmt->bindParam(':adulte', $adulte);
            $stmt->execute();
            
            return $db->lastInsertId();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }

    // Mise à jour d'un adulte
    public function updateEleve(/* Eleve */ $eleve, /* myPDO */ $db) {
        try {

            $stmt = $db->prepare("UPDATE Etudiant SET nom=:nom, prenom=:prenom, imgProfil=:img WHERE identifiant=:id ;");
            $nom = utf8_decode($eleve->getNom());

            $prenom = utf8_decode($eleve->getPrenom());
            $mdp = utf8_decode($eleve->getPassword());
            $img = utf8_decode($eleve->getImage());
            $id = $eleve->getIdentifiant();
            $stmt->bindParam(':nom', $nom);
            $stmt->bindParam(':prenom', $prenom);
            $stmt->bindParam(':img', $img);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }
    
    // Mise à jour d'un élève n'ayant pas de photo
    public function updateEleveSansPhoto(/* Eleve */ $eleve, /* myPDO */ $db) {
        try {

            $stmt = $db->prepare("UPDATE Etudiant SET nom=:nom, prenom=:prenom WHERE identifiant=:id ;");
            $nom = utf8_decode($eleve->getNom());
            $prenom = utf8_decode($eleve->getPrenom());
            $mdp = utf8_decode($eleve->getPassword());
            $id = $eleve->getIdentifiant();
            $stmt->bindParam(':nom', $nom);
            $stmt->bindParam(':prenom', $prenom);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }
    
    // Trouver un élève selon son id
    public function findEleve(/* Eleve */ $eleve, /* myPDO */ $db) {
        $stmt = $db->prepare("SELECT * FROM Etudiant WHERE identifiant = :id ;");
        $id = $eleve->getIdentifiant();
        $stmt->bindParam(':id', $id);
        $res = $stmt->execute();
        $row = $stmt->fetchObject();
        if ($row == null) {
            return null;
        } else {
            //print_r($row);
            $eleve = new Eleve($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom), utf8_encode($row->mdp), utf8_encode($row->imgProfil), utf8_encode($row->Adulte_identifiant));
            return $eleve;
        }
    }
    
    // Trouver un élève selon le nom et prenom 
    public function find ( $eleve , $db) {
        $stmt = $db->prepare("SELECT * FROM Etudiant WHERE nom = :nom AND prenom = :prenom;");
        $nom = utf8_decode($eleve->getNom());
        $prenom = utf8_decode($eleve->getPrenom());
        $stmt->bindParam(':nom', $nom);
        $stmt->bindParam(':prenom', $prenom);
        $res = $stmt->execute();
        $row = $stmt->fetchObject();
        if ($row == null) {
            return 0;
        } else {
          
            return 1;
        }
    }
    
    // Trouver les élèves n'appartenant pas à un adulte
    public function findOtherEleve(/* Eleve */ $eleve, /* myPDO */ $db) {
		$stmt = $db->prepare("SELECT * FROM Etudiant WHERE identifiant != :id AND enom = :nom AND prenom = :prenom;");
		$id = $eleve->getIdentifiant();
                $nom = utf8_decode($eleve->getNom());
                $prenom = utf8_decode($eleve->getPrenom());
		$stmt->bindParam(':id', $id);
		$stmt->bindParam(':nom', $nom);
                $stmt->bindParam(':prenom', $prenom);
		$res = $stmt->execute();
		$row = $stmt->fetchObject();
		if($row == null) {
			return 0 ;
		}
		else {
                    return 1;
		}
	}
        
    // Trouver tous les élèves 
    public function findAllEleves(/* myPDO */ $db) {
        $tabRes = array();
        $i = 0;
        $stmt = $db->prepare("SELECT * FROM Etudiant ;");
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $eleve = new Eleve($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom), utf8_encode($row->mdp), utf8_encode($row->imgProfil), utf8_encode($row->Adulte_identifiant));

            $tabRes[$i] = $eleve;
            $i++;
        }
        return $tabRes;
    }
    
     // trouver les élèves appartenant à un adulte
     public function findAllElevesP($adulte, $db) {
        $tabRes = array();
        $i = 0;
        
        $idAd = $adulte->getIdentifiant();
        
        $stmt = $db->prepare("SELECT * FROM Etudiant WHERE Adulte_identifiant = :idAd;");
        $stmt->bindParam(':idAd', $idAd);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $eleve = new Eleve($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom), utf8_encode($row->mdp), utf8_encode($row->imgProfil), utf8_encode($row->Adulte_identifiant));

            $tabRes[$i] = $eleve;
            $i++;
        }
        return $tabRes;
    }
    
     // Recherche des étudiant appartenant à un adulte
     public function Rechercher($adulte,$eleve, $db) {
        $tabRes = array();
        $i = 0;
        
        $idAd = $adulte->getIdentifiant();
        $nom = $eleve->getNom();
        $t = $nom."%";
        $stmt = $db->prepare("SELECT * FROM Etudiant WHERE Adulte_identifiant = :idAd  AND ( nom LIKE :nom OR prenom LIKE :nom OR CONCAT (nom,' ',prenom) LIKE :nom OR CONCAT (prenom,' ',nom) LIKE :nom)  ;");
        $stmt->bindParam(':idAd', $idAd);
        $stmt->bindParam(':nom', $t);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $eleve = new Eleve($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom), utf8_encode($row->mdp), utf8_encode($row->imgProfil), utf8_encode($row->Adulte_identifiant));

            $tabRes[$i] = $eleve;
            $i++;
        }
        return $tabRes;
    }
       
    // Trouver l'élève selon son nom , prénom et mot de passe
    public function findPersonne(/* Personne */ $eleve, /* myPDO */ $db) {
        $stmt = $db->prepare("SELECT * FROM Etudiant WHERE nom = :nom AND prenom = :prenom AND mdp = :mdp ; ;");
        $prenom = utf8_decode($eleve->getPrenom());
        $nom = utf8_decode($eleve->getNom());
        $mdp = utf8_decode($eleve->getPassword());
        $stmt->bindParam(':prenom', $prenom);
        $stmt->bindParam(':nom', $nom);
        $stmt->bindParam(':mdp', $mdp);
        $res = $stmt->execute();
        $row = $stmt->fetchObject();
        if ($row == null) {
            return null;
        } else {
           
            $pers = new Eleve($row->identifiant, utf8_encode($row->nom), utf8_encode($row->prenom), utf8_encode($row->mdp), utf8_encode($row->imgProfil), utf8_encode($row->Adulte_identifiant));
            return $pers;
        }
    }

    /*
    public function displayAllEleves( $db) {
        $stmt = $db->prepare("SELECT * FROM Etudiant ;");
        $res = $stmt->execute();
        echo "<table>";
        while ($row = $stmt->fetchObject()) {
            echo "<tr>";
            echo "<td>" . $row->identifiant . "</td>";
            echo "<td>" . $row->nom . "</td>";
            echo "<td>" . $row->prenom . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
     * 
     */

    // Supprimer un élève
    public function deleteEleve(/* Eleve */ $eleve, /* myPDO */ $db) {
        try {
            $stmt = $db->prepare("DELETE FROM Etudiant WHERE identifiant = :id ;");
            $id = $eleve->getIdentifiant();
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }
    
 
}

/* $t = new QuestionDAO() ;
  $question = new Question(1, "Qe", null, 1);
  $db = new myPDO("127.0.0.1", "root", "", 0);
  //$t->createQuestion($question, $db);
  //$t->displayAllQuestions($db);

  $tabRes = $t->findAllQuestions($db);
  echo "<br/>";
  echo "<pre>";
  print_r($tabRes);
  echo "</pre>";
  echo "<br/>";
 */
?>